﻿/**
 *
 * at_client.h
 *
 *  Created on: 2019-11-26
 *      Author: chenshisheng
 */

#ifndef AT_COMMAND_H_
#define AT_COMMAND_H_

#include "main.h"
#include "message_queue.h"
#include "shell.h"

typedef enum
{
    AtCommand_DateType_Min = 1,

    AtCommand_DataType_String = AtCommand_DateType_Min,
    AtCommand_DataType_Opaque,
    AtCommand_DataType_Integer,
    AtCommand_DataType_Float,
    AtCommand_DataType_Bool,
    AtCommand_DataType_HexString,

    AtCommand_DateType_Max
}AtCommand_DataType_t;

typedef union
{
    int iv;
    float fv;
    bool bv;
    const char *sv;
}AtCommand_Value_t;

typedef enum
{
    AtCommand_WriteResult_Success = 2,
    AtCommand_WriteResult_BadRequest = 11,
    AtCommand_WriteResult_Unauthorized,
    AtCommand_WriteResult_NotFound,
    AtCommand_WriteResult_MethodNotAllowed,
}AtCommand_WriteResult_t;

typedef enum
{
    AtCommand_MiplEvent_BootstrapSuccess = 2,
    AtCommand_MiplEvent_BootstrapFail,
    AtCommand_MiplEvent_RegisterSuccess = 6,
    AtCommand_MiplEvent_RegisterFail,
    AtCommand_MiplEvent_UpdateSuccess = 11,
    AtCommand_MiplEvent_CancleSuccess = 15,
    AtCommand_MiplEvent_NotifyAck = 26,
}AtCommand_MiplEvent_t;

typedef enum
{
    AtCommand_NetworkRegState_Searching,
    AtCommand_NetworkRegState_HomeRegistered,
    AtCommand_NetworkRegState_Denied,
    AtCommand_NetworkRegState_Unknown,
    AtCommand_NetworkRegState_RoamingRegistered,
}AtCommand_NetworkRegState_t;

typedef struct
{
    int mid;
    int objId;
    int insId;
    int resId;
    AtCommand_DataType_t type;
    int len;
    char *valueString;
}AtCommand_WriteInfo_t;

typedef AtCommand_WriteResult_t (*AtCommand_WriteHandler)(const AtCommand_WriteInfo_t *info);

void AtCommand_Init(AtCommand_WriteHandler writer);
void AtCommand_OnEvent(const MsgQueue_Msg_t *msg);

int AtCommand_At(void);
int AtCommand_Reboot(void);
int AtCommand_CSQ(void);
int AtCommand_CSCON(int n);
AtCommand_NetworkRegState_t AtCommand_ReadNetworkRegState(void);
int AtCommand_MiplCreate(void);
int AtCommand_MiplDelete(void);
int AtCommand_MiplAutoUpdate(bool isEnable);
int AtCommand_MiplAddObj(int objId, int insCount);
int AtCommand_MiplDelObj(int objId);
int AtCommand_MiplDiscoverRsp(int objId, const int *resIds, unsigned int resCound);
int AtCommand_MiplOpen(int lifetime, int timeout);
int AtCommand_MiplClose(void);
int AtCommand_MiplUpdate(int lifetime);
int AtCommand_MiplNotify(
        int objId,
        int insId,
        int resId,
        AtCommand_DataType_t type,
        AtCommand_Value_t value);
char * AtCommand_ReadIMEI(void);
char * AtCommand_ReadIMSI(void);

const char *AtCommand_DateTypeString(AtCommand_DataType_t type);
int AtCommand_ReadIdCmd(Stream_t *stream, int argc, char *argv[]);
int AtCommand_ShellCmd(Stream_t *stream, int argc, char *argv[]);

#endif /* AT_COMMAND_H_ */
